package com.honeywen.mymvc.debt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wangwei
 * @date 2020/9/23
 */
public class TestOrder {


    public static final int max = 10;


    public static void dispatch() {

        List<Map<String, Object>> createByNum = generateDate();
        Collections.reverse(createByNum);

        createByNum = createByNum.stream().filter(i -> Integer.valueOf(i.get("applyNum").toString()) < max).collect(Collectors.toList());



        // 循环整理结果
        List<Map<String, Object>> countPerMap = new ArrayList<>();


        if (createByNum != null && createByNum.size() > 0) {

            int mini = 0;


            for (int i = 0; i < createByNum.size(); i++) {

                Map<String, Object> temp = new HashMap<>();
                temp.put("nums", 0);
                temp.put("userList", new ArrayList<>());
                countPerMap.add(temp);

                for (int j = i + 1; j < createByNum.size(); j++) {
                    int b = Integer.parseInt(createByNum.get(j).get("applyNum").toString());
                    int a = Integer.parseInt(createByNum.get(i).get("applyNum").toString());

                    int diff = b - a;

                    if (diff > 0) {

                        // j 位置之前的都加上diff


                    } else if (diff == 0) {

                    }


                }

            }

            for (Map<String, Object> map : createByNum) {


                Integer tempNum = Integer.valueOf(map.get("applyNum").toString());
                if (max <= tempNum) {
                    continue;
                }




                tempNum = max - tempNum - mini;
                if (tempNum > 0) {
                    mini = mini + tempNum;
                    Map<String, Object> temp = new HashMap<>();
                    temp.put("nums", tempNum);
                    temp.put("userList", new ArrayList<>());
                    countPerMap.add(temp);
                }

                Map<String, String> temp = new HashMap<>();
                temp.put("id", map.get("createBy").toString());
                temp.put("name", map.get("collectUser").toString());
                for (Map<String, Object> list : countPerMap) {
                    ((List) list.get("userList")).add(temp);
                }
            }
        }



    }


    public static void main(String[] args) {


        List<Map<String, Object>> createByNum = generateDate();

        // 循环整理结果
        List<Map<String, Object>> countPerMap = new ArrayList<>();
        if (createByNum != null && createByNum.size() > 0) {
            int mini = 0;
            for (Map<String, Object> map : createByNum) {

                Integer tempNum = Integer.valueOf(map.get("applyNum").toString());

                if (max > tempNum) {
                    tempNum = max - tempNum - mini;
                    if (tempNum > 0) {
                        mini = mini + tempNum;
                        Map<String, Object> temp = new HashMap<>();
                        temp.put("nums", tempNum);
                        temp.put("userList", new ArrayList<>());
                        countPerMap.add(temp);
                    }

                    Map<String, String> temp = new HashMap<>();
                    temp.put("id", map.get("createBy").toString());
                    temp.put("name", map.get("collectUser").toString());
                    for (Map<String, Object> list : countPerMap) {
                        ((List) list.get("userList")).add(temp);
                    }
                }
            }
        }


        System.out.println(countPerMap);
        System.out.println(JSON.toJSON(countPerMap).toString());



    }

    private static List<Map<String, Object>> generateDate() {

        List<Map<String, Object>> list = Lists.newArrayList();

        Map<String, Object> map1 = Maps.newHashMap();
        map1.put("createBy", "1");
        map1.put("collectUser", "jack");
        map1.put("applyNum", 7);
        list.add(map1);

        Map<String, Object> map2 = Maps.newHashMap();
        map2.put("createBy", "2");
        map2.put("collectUser", "rose");
        map2.put("applyNum", 3);
        list.add(map2);

        Map<String, Object> map3 = Maps.newHashMap();
        map3.put("createBy", "3");
        map3.put("collectUser", "bob");
        map3.put("applyNum", 1);
        list.add(map3);

        Map<String, Object> map4 = Maps.newHashMap();
        map4.put("createBy", "4");
        map4.put("collectUser", "allen");
        map4.put("applyNum", 1);
        list.add(map4);

//        Map<String, Object> map5 = Maps.newHashMap();
//        map5.put("createBy", "5");
//        map5.put("collectUser", "pony");
//        map5.put("applyNum", 4);
//        list.add(map5);
//
//        Map<String, Object> map6 = Maps.newHashMap();
//        map6.put("createBy", "6");
//        map6.put("collectUser", "robin");
//        map6.put("applyNum", 3);
//        list.add(map6);
//
//        Map<String, Object> map7 = Maps.newHashMap();
//        map7.put("createBy", "7");
//        map7.put("collectUser", "leon");
//        map7.put("applyNum", 2);
//        list.add(map7);
//
//        Map<String, Object> map8 = Maps.newHashMap();
//        map8.put("createBy", "8");
//        map8.put("collectUser", "lily");
//        map8.put("applyNum", 1);
//        list.add(map8);


        return list;

    }
}
